
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       mb_landing.c
  * @author     baiyang
  * @date       2023-7-13
  ******************************************************************************
  */

/*----------------------------------include-----------------------------------*/
#include "mb_landing.h"
/*-----------------------------------macro------------------------------------*/

/*----------------------------------typedef-----------------------------------*/

/*---------------------------------prototype----------------------------------*/

/*----------------------------------variable----------------------------------*/
struct mb_landing fw_landing;
/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
bool landing_is_flaring(void)
{
    if (!fw_landing.flags.in_progress) {
        return false;
    }

    switch (fw_landing.type) {
    case LANDING_TYPE_STANDARD_GLIDE_SLOPE:
        return landing_type_slope_is_flaring();
#if HAL_LANDING_DEEPSTALL_ENABLED
    case LANDING_TYPE_DEEPSTALL:
#endif
    default:
        return false;
    }
}

// return true while the aircraft is performing a landing approach
// when true the vehicle will:
//   - disable ground steering
//   - call setup_landing_glide_slope() and adjust_landing_slope_for_rangefinder_bump()
//   - will be considered flying if sink rate > 0.2, and can trigger crash detection
bool landing_is_on_approach(void)
{
    if (!fw_landing.flags.in_progress) {
        return false;
    }

    switch (fw_landing.type) {
    case LANDING_TYPE_STANDARD_GLIDE_SLOPE:
        return landing_type_slope_is_on_approach();
#if HAL_LANDING_DEEPSTALL_ENABLED
    case LANDING_TYPE_DEEPSTALL:
        return deepstall.is_on_approach();
#endif
    default:
        return false;
    }
}

// return true while the aircraft is allowed to perform ground steering
bool landing_is_ground_steering_allowed(void)
{
    if (!fw_landing.flags.in_progress) {
        return true;
    }

    switch (fw_landing.type) {
    case LANDING_TYPE_STANDARD_GLIDE_SLOPE:
        return landing_type_slope_is_on_approach();
#if HAL_LANDING_DEEPSTALL_ENABLED
    case LANDING_TYPE_DEEPSTALL:
        return false;
#endif
    default:
        return true;
    }
}

bool landing_type_slope_is_flaring(void)
{
    return (fw_landing.type_slope_stage == LDSS_FINAL);
}

bool landing_type_slope_is_on_approach(void)
{
    return (fw_landing.type_slope_stage == LDSS_APPROACH ||
            fw_landing.type_slope_stage == LDSS_PREFLARE);
}

bool landing_type_slope_is_expecting_impact(void)
{
    return (fw_landing.type_slope_stage == LDSS_PREFLARE ||
            fw_landing.type_slope_stage == LDSS_FINAL);
}

bool landing_type_slope_is_complete(void)
{
    return (fw_landing.type_slope_stage == LDSS_FINAL);
}

// return true when at the last stages of a land when an impact with the ground is expected soon
// when true is_flying knows that the vehicle was expecting to stop flying, possibly because of a hard impact
bool landing_is_expecting_impact(void)
{
    if (!fw_landing.flags.in_progress) {
        return false;
    }

    switch (fw_landing.type) {
    case LANDING_TYPE_STANDARD_GLIDE_SLOPE:
        return landing_type_slope_is_expecting_impact();
#if HAL_LANDING_DEEPSTALL_ENABLED
    case LANDING_TYPE_DEEPSTALL:
#endif
    default:
        return false;
    }
}

// returns true when the landing library has overriden any servos
bool landing_override_servos(void)
{
    if (!fw_landing.flags.in_progress) {
        return false;
    }

    switch (fw_landing.type) {
#if HAL_LANDING_DEEPSTALL_ENABLED
    case LANDING_TYPE_DEEPSTALL:
        return deepstall.override_servos();
#endif
    case LANDING_TYPE_STANDARD_GLIDE_SLOPE:
    default:
        return false;
    }
}

int16_t landing_get_pitch_cd(void)
{
    return fw_landing.pitch_cd;
}

float landing_get_flare_sec(void)
{
    return fw_landing.flare_sec;
}

int8_t landing_get_disarm_delay(void)
{
    return fw_landing.disarm_delay;
}

int8_t landing_get_then_servos_neutral(void)
{
    return fw_landing.then_servos_neutral;
}

int8_t landing_get_abort_throttle_enable(void)
{
    return fw_landing.abort_throttle_enable;
}

int8_t landing_get_flap_percent(void)
{
    return fw_landing.flap_percent;
}

int8_t landing_get_throttle_slewrate(void)
{
    return fw_landing.throttle_slewrate;
}

bool landing_is_commanded_go_around(void)
{
    return fw_landing.flags.commanded_go_around;
}

void landing_set_initial_slope(void)
{
    fw_landing.initial_slope = fw_landing.slope;
}
/*------------------------------------test------------------------------------*/


